from notify import send
import requests
import time
import random

url_1='https://api.livelab.com.cn/performance/app/project/get_performs?project_id=572&retry=false'
headers_1={'user-agent':'Mozilla/5.0 (Linux; Android 11; M2104K10AC Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.4389.72 MQQBrowser/6.2 TBS/046237 Mobile Safari/537.36 QQLiveBrowser/8.7.85.27058',
           'Content-Type':'application/json',
           'Referer':'https://servicewechat.com/wx5a8f481d967649eb/79/page-frame.html'}
def get_seat_plan_id():
    try:
        response_1 = requests.get(url_1,headers=headers_1).json()
        print(response_1)
        seat_plans_length = len(response_1["data"]["performInfos"][0]["performInfo"][0]["seatPlans"])
        seat_Plan_Ids = []
        seat_Plan_Names = []
        IS = []
        numbers = []
        for i in range(seat_plans_length):
            seat_Plan_Id = response_1["data"]["performInfos"][0]["performInfo"][0]["seatPlans"][i]["seatPlanId"]
            seat_Plan_Name = response_1["data"]["performInfos"][0]["performInfo"][0]["seatPlans"][i]["seatPlanName"]
            seat_Plan_Ids.append(str(seat_Plan_Id))
            seat_Plan_Names.append(seat_Plan_Name)
        result_str = ",".join(seat_Plan_Ids)
        url_2='https://api.livelab.com.cn/performance/app/project/seatPlanStatus?seatPlanIds='+result_str+'&type=3'
        response_2 = requests.get(url_2,headers=headers_1).json()
        for i in range(seat_plans_length):
            sold_Out_Flag = response_2["data"][i]['soldOutFlag']
            if sold_Out_Flag == False:
                IS.append(i)
        for j in IS:
            seat_Plan_Name = seat_Plan_Names[j]
            numbers.append(seat_Plan_Name)
            print(seat_Plan_Name)
        result = ",".join(numbers)
        if result == "":
            result = "无票"
            print(result)
        else:
            send("梁静茹杭州演唱会", f"{result}有票")
    except Exception as e:
        print(e)
        send("梁静茹杭州演唱会", f"报错:{e}")
if __name__ == '__main__':
    rand=round(random.uniform(0, 2), 2)
    print(f'等待{rand}秒后运行')
    time.sleep(rand)
    get_seat_plan_id()